﻿<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Bieb.Domain"
                   namespace="Bieb.Domain.Entities">

  <class name="Publisher">
    
    <id name="Id" column="PublisherId">
      <generator class="native" />
    </id>

    <version name="ModifiedDate" unsaved-value="null" generated="always" type="DateTime2">
      <column name="ModifiedDate" not-null="false" sql-type="DATETIME2" />
    </version>
    <property name="CreatedDate" generated="always" type="DateTime2">
      <column name="CreatedDate" not-null="false" sql-type="DATETIME2" />
    </property>
    
    <property name="Name" not-null="true" index="IX_Publisher_Name"/>

    <set name="Books" access="field.camelcase">
      <key column="PublisherId" foreign-key="FK_Book_Publisher" />
      <one-to-many class="Book" />
    </set>

    <set name="Stories" table="Story" access="field.camelcase">
      <key column="PublisherId" foreign-key="FK_Story_Publisher" />
      <one-to-many class="Story" />
    </set>
    
  </class>

  <database-object>
    <create>
      GO
      CREATE TRIGGER TR_PublisherInserted ON Publisher AFTER INSERT AS
      BEGIN
        SET NOCOUNT ON
        
        UPDATE Publisher 
        SET    ModifiedDate = GetDate(), CreatedDate = GetDate() 
        FROM   inserted 
        WHERE  Publisher.PublisherId = inserted.PublisherId
        
        SET NOCOUNT OFF
      END
      GO
      CREATE TRIGGER TR_PublisherUpdated ON Publisher AFTER UPDATE AS
      BEGIN
        SET NOCOUNT ON
        
        UPDATE Publisher 
        SET    ModifiedDate = GetDate() 
        FROM   inserted 
        WHERE  Publisher.PublisherId = inserted.PublisherId
        
        SET NOCOUNT OFF
      END
      GO
    </create>
    <drop>
    </drop>
  </database-object>
  
</hibernate-mapping>